package com.lzdongrui.backend.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.lzdongrui.backend.domain.College;
import com.lzdongrui.backend.domain.dto.CollegeDTO;
import com.lzdongrui.backend.domain.dto.MajorDTO;
import com.lzdongrui.backend.domain.vo.MajorVO;
import com.lzdongrui.common.annotation.Anonymous;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lzdongrui.common.annotation.Log;
import com.lzdongrui.common.core.controller.BaseController;
import com.lzdongrui.common.core.domain.AjaxResult;
import com.lzdongrui.common.enums.BusinessType;
import com.lzdongrui.backend.domain.Major;
import com.lzdongrui.backend.service.IMajorService;
import com.lzdongrui.common.utils.poi.ExcelUtil;
import com.lzdongrui.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 专业Controller
 * 
 * @author wyh
 * @date 2023-05-05
 */
@RestController
@RequestMapping("/backend/major")
public class MajorController extends BaseController
{
    @Autowired
    private IMajorService majorService;

    /**
     * 查询专业列表
     */
    @PreAuthorize("@ss.hasPermi('backend:major:list')")
    @GetMapping("/list")
    public TableDataInfo list(MajorDTO majorDTO)
    {
        startPage();
        List<MajorVO> list = majorService.selectMajorList(majorDTO);
        return getDataTable(list);
    }

    /**
     * 查询专业列表
     */
    @GetMapping("/all-list")
    public TableDataInfo allList(MajorDTO majorDTO)
    {
        List<Major> list = majorService.selectMajorAllList(majorDTO);
        return getDataTable(list);
    }

    /**
     * 导出专业列表
     */
    @PreAuthorize("@ss.hasPermi('backend:major:export')")
    @Log(title = "专业", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, MajorDTO majorDTO)
    {
        List<MajorVO> list = majorService.selectMajorList(majorDTO);
        ExcelUtil<MajorVO> util = new ExcelUtil<MajorVO>(MajorVO.class);
        util.exportExcel(response, list, "专业数据");
    }

    /**
     * 获取专业详细信息
     */
    @PreAuthorize("@ss.hasPermi('backend:major:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(majorService.selectMajorById(id));
    }

    /**
     * 新增专业
     */
    @PreAuthorize("@ss.hasPermi('backend:major:add')")
    @Log(title = "专业", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Major major)
    {
        major.setOperator(getUserId());
        return toAjax(majorService.insertMajor(major));
    }

    /**
     * 修改专业
     */
    @PreAuthorize("@ss.hasPermi('backend:major:edit')")
    @Log(title = "专业", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Major major)
    {
        major.setOperator(getUserId());
        return toAjax(majorService.updateMajor(major));
    }

    /**
     * 删除专业
     */
    @PreAuthorize("@ss.hasPermi('backend:major:remove')")
    @Log(title = "专业", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(majorService.deleteMajorByIds(ids));
    }

    /**
     * 下载模板
     *
     * @param response
     */
    @Anonymous
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<MajorDTO> util = new ExcelUtil<MajorDTO>(MajorDTO.class);
        util.importTemplateExcel(response, "专业信息");
    }

    /**
     * 导入学院数据
     *
     * @param file
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "学院", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('backend:college:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        Long Operator = getUserId();
        ExcelUtil<MajorDTO> util = new ExcelUtil<MajorDTO>(MajorDTO.class);
        List<MajorDTO> majorDTOS = util.importExcel(file.getInputStream());
        String message = majorService.importMajor(majorDTOS, updateSupport,Operator);
        return success(message);
    }
}
